-module(register_adapter).
-include("rec_dec.hrl").
-export([newReg/1,simpleReg/1,toHtml/2,confirm/1,get/2,delete/1,quick_create/1]).

newReg(L) ->
	VReg = vregister_service:addchange(
		#v_register_rec{
			id=tools:get_param(L,"id",""),
			name=tools:get_param(L,"name",""),
			mobile=tools:get_param(L,"mobile",""),
			city=tools:get_param(L,"city",""),
			addr=tools:get_param(L,"addr",""),
			password=tools:get_param(L,"password",""),
			company = tools:get_param(L,"company",""),
			other=tools:get_param(L,"other","")}
	),

	U = user_service:addchange(VReg),

	user_adapter:auth(admin,{U#user_rec.login_name,U#user_rec.password,first_login}).


quick_create(L) ->
	case session_service:getLoginedInfo(L,state) of
	first_login ->
		[Reg|_] = vregister_service:get({id,session_service:getLoginedInfo(L,register_id)}),
		vendor_adapter:addchange({true,
			#vendor_rec
			{
				id = "",
				name = Reg#v_register_rec.company,
				city = Reg#v_register_rec.city,
				contactor = Reg#v_register_rec.name,
				addr = Reg#v_register_rec.addr,
				register_id = Reg#v_register_rec.id,
				mobile_phone = Reg#v_register_rec.mobile
			}}
		);
	_ ->
		ok
	end,
	{redirect_local,"/www/pages/vendor_mgr.yaws"}.

simpleReg(L) ->
	VReg = vregister_service:addchange(
		#v_register_rec{
			id=tools:get_param(L,"id",""),
			name=tools:get_param(L,"name",""),
			mobile=tools:get_param(L,"mobile",""),
			city=tools:get_param(L,"city",""),
			addr=tools:get_param(L,"addr",""),
			password=tools:get_param(L,"password",""),
			company = tools:get_param(L,"company",""),
			other=tools:get_param(L,"other","")}
	),

	vendor_adapter:addchange({true,
		#vendor_rec
		{
			id = VReg#v_register_rec.id,
			name = VReg#v_register_rec.company,
			city = VReg#v_register_rec.city,
			contactor = VReg#v_register_rec.name,
			addr = VReg#v_register_rec.addr,
			register_id = VReg#v_register_rec.id
		}}
	),

	user_service:addchange(VReg),
	{redirect_local,"/www/login.yaws"}.
delete(L) ->
	case tools:get_param(L,"register_id","") of
	"" ->
		{html,"no id information"};
	Id ->
		vregister_service:delete(list_to_binary(Id)),
		{html,"ok"}

	end.
get(html_list,L) ->
	{ehtml,
	{'div',[],lists:map(
					fun(E) ->
						toHtml(list,E)
					end,
					case tools:get_param(L,"register_id","") of
					"" ->
						vregister_service:get();
					Id ->
						vregister_service:get(list_to_binary(Id))
					end
			)
	}}.
toHtml(list,E) ->
	{'div',[],[
			{a,[{href,io_lib:format("/delete_register.html?register_id=~s",[binary_to_list(E#v_register_rec.id)])}],E#v_register_rec.name},
			E#v_register_rec.mobile,
			{a,[{href,io_lib:format("/www/unauth_act.yaws?mod=register&action=delete&register_id=~s",[binary_to_list(E#v_register_rec.id)])}],"删除"}
			]
	};
toHtml(edit,E) when is_record(E, v_register_rec) ->
	{form,[],[{table,[],[
		{tr,[],[{td,[],"手机"},{td,[],[{input,[{value,E#v_register_rec.mobile}],[]}]}]},
		{tr,[],[{td,[],"姓名"},{td,[],[{input,[{value,E#v_register_rec.name}],[]}]}]},
		{tr,[],[{td,[],"公司名称"},{td,[],[{input,[{value,""}],[]}]}]},
		{tr,[],[{td,[],"城市"},{td,[],[{input,[{value,E#v_register_rec.city}],[]}]}]},
		{tr,[],[{td,[],"地址"},{td,[],[{input,[{value,E#v_register_rec.addr}],[]}]}]}
	]}]}.
confirm(L) ->
	Mobile = tools:get_param(L,"mobile",""),
	case gen_server:call(confirm_service,{send_sms,Mobile}) of
	{ok,Token} ->
		user_adapter:auth(temp_reg,{Mobile,Token});
	_ ->
		[{redirect_local,"/www/login.yaws"}]
	end.

	